package cl.parser.internal;

import java.util.ArrayList;
import java.util.List;

import cl.parser.IParseNode;
import cl.parser.external.CloseBraceNode;
import cl.parser.external.OpenBraceNode;

public class CodeBlockNode extends InternalNode implements IFat {

    @Override
    public String prettyPrint(String indent) {
        String res = "{\n";
        
        for(IParseNode p : children) {
            res += p.prettyPrint(indent + "    ");
        }
        
        if(children.size() > 0) {
            return res + indent + "}";
        } else {
            // Usually follow empty functions
            return "{ }\n";
        }
    }
    
    @Override
    protected List<IParseNode> getAllowed(IParseNode last, List<IParseNode> soFar) {
        List<IParseNode> allowed = new ArrayList<IParseNode>();
        
        if(last == null) {
            allowed.add(new OpenBraceNode());
        } else if(last instanceof OpenBraceNode ||
                last instanceof StatementNode ||
                last instanceof CodeBlockNode) {
            allowed.add(new StatementNode());
            allowed.add(new CloseBraceNode());
            allowed.add(new CodeBlockNode());
        }
        
        
        return allowed;
    }

    @Override
    protected boolean isValid(IParseNode last, List<IParseNode> soFar) {
        return last instanceof CloseBraceNode;
    }

    @Override
    public void loseWeight() {
        // remove { }
        children.remove(0);
        children.remove(children.size() - 1);
    }

}
